package com.shop.service.pay.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.shop.common.TokenUtil;
import com.shop.entity.BaseOrderDo;
import com.shop.entity.ClientDo;
import com.shop.entity.PayBeanDo;
import com.shop.entity.SystemParameterDo;
import com.shop.mapper.ClientDoMapper;
import com.shop.service.pay.AbService;
import com.shop.service.pay.PayService;
import com.shop.shopEnum.SystemParameterEnum;
import com.shop.vo.PaymentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

@Service
public class AliPayImpl extends AbService implements PayService {

    @Autowired
    private ClientDoMapper clientDoMapper;

    @Override
    public Object payment(PaymentVo paymentVo) throws Exception {
        /*  /* 1、获得初始化的 AlipayClient 参数 */
        Map<String, List<SystemParameterDo>> map = super.payParam(SystemParameterEnum.ALIPAY.code);
        if (map.size() <= 0 ) throw new Exception("AlipayClient未查询到");
        /* 2、AlipayClient 初始化  */
        AlipayClient alipayClient = new DefaultAlipayClient(
                map.get("gatewayUrl").get(0).getSystemValue(),
                map.get("appId").get(0).getSystemValue(),
                map.get("privateKey").get(0).getSystemValue(),
                map.get("format").get(0).getSystemValue(),
                map.get("charset").get(0).getSystemValue(),
                map.get("publicKey").get(0).getSystemValue(),
                map.get("signType").get(0).getSystemValue());


        /* 3、设置请求参数 */
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        /* 4、页面跳转同步通知页面路径 */
        alipayRequest.setReturnUrl(map.get("returnUrl").get(0).getSystemValue());
        /* 5、服务器异步通知页面路径 */
        alipayRequest.setNotifyUrl(map.get("notifyUrl").get(0).getSystemValue());
        /* 6、封装参数 */
        alipayRequest.setBizContent(JSON.toJSONString( this.requestData(paymentVo, map.get("timeoutExpress").get(0).getSystemValue())));
        /* 7、请求支付宝进行付款，并获取支付结果 */
        return alipayClient.pageExecute(alipayRequest);
    }

    /**
     * 组装请求PayBeanDo参数
     * @param paymentVo
     * @return
     * @throws Exception
     */
    private PayBeanDo requestData(PaymentVo paymentVo,String timeoutExpress) throws Exception {
        BaseOrderDo baseOrderDo = super.getBaseOrderDo(paymentVo.getPkBaseOrderId());
        Assert.notNull(baseOrderDo, "没有找到订单！");
        ClientDo clientDo = clientDoMapper.selectById(TokenUtil.getUserId());
        Assert.notNull(clientDo, "没有找到客户！");
        PayBeanDo payBeanDo = new PayBeanDo();
        payBeanDo.setOut_trade_no(baseOrderDo.getBaseOrderNo());
        payBeanDo.setSubject(String.format("%s购物支付", clientDo.getClientName()));
        payBeanDo.setTotal_amount(String.valueOf(baseOrderDo.getPaymentMoney()));
        payBeanDo.setTimeout_express(timeoutExpress);
        return payBeanDo;
    }


}
